﻿using CipherFactory;
using IR.Common.Utilities;
using IR.Controller.ModelControllers;
using IR.WPF.UI.Controls;
using IR.WPF.ViewModels.LandingViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace IR.WPF.UI.ViewModels
{
    public class VMUserEdit : ViewModelBase
    {
        private CUser _userToEdit;
        private string _typeOfPage;
        private bool _isOld;
        private ICommand _cancelCommand;
        private ICommand _saveCommand;

        private string initialPK;

        public VMUserEdit(CUser userToEdit = null)
        {
            if (userToEdit == null)
            {
                _userToEdit = new CUser();
                _isOld = false;
                _typeOfPage = "New User";
            }
            else
            {
                _userToEdit = userToEdit;
                _isOld = true;
                initialPK = _userToEdit.Username;
                _typeOfPage = "Edit User";
            }
        }

        #region **Properties**
        public string TypeOfPage
        {
            get { return _typeOfPage; }
            set { _typeOfPage = value; }
        }

        public string Username
        {
            get { return _userToEdit.Username; }
            set { _userToEdit.Username = value; }
        }

        public string FName
        {
            get { return _userToEdit.FName; }
            set { _userToEdit.FName = value; }
        }

        public string LName
        {
            get { return _userToEdit.LName; }
            set { _userToEdit.LName = value; }
        }

        public string Email
        {
            get { return _userToEdit.Email; }
            set { _userToEdit.Email = value; }
        }
        #endregion

        #region **Commands**
        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Cancel
        {
            get
            {
                if (_cancelCommand == null)
                    _cancelCommand = new RelayCommand(p => CancelFunction(p as Control), p => CanCancel(p as Control));

                return _cancelCommand;
            }
        }

        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Save
        {
            get
            {
                if (_saveCommand == null)
                    _saveCommand = new RelayCommand(p => SaveFunction(p as Control), p => CanSave(p as Control));

                return _saveCommand;
            }
        }
        #endregion

        #region **Command Functions**
        protected void CancelFunction(Control sender)
        {
            // Get a reference to the Window that contained the sender Control
            Window window = Window.GetWindow(sender);

            window.Close();
        }

        protected void SaveFunction(Control sender)
        {
            bool canSave = true;
            bool saved = false;
            bool inputError = false;
            Grid primaryGrid = (BoundWindow.FindName("gridControlHost") as Grid);
            UserEdit userControl = primaryGrid.Children[0] as UserEdit;
            Grid myGrid = userControl.FindName("MyGrid") as Grid;
            PasswordBox passwordBox = myGrid.FindName("Password") as PasswordBox;

            if (_isOld == true)
            {
                if (initialPK != _userToEdit.Username)
                    canSave = false;
            }
            else
            {
                if (string.IsNullOrEmpty(passwordBox.Password))
                {
                    MessageBox.Show("You must enter a password for a new user.");
                    inputError = true;
                }
            }

            // Check to make sure nothing is null
            if (string.IsNullOrEmpty(_userToEdit.Username)
                || string.IsNullOrEmpty(_userToEdit.FName)
                || string.IsNullOrEmpty(_userToEdit.LName)
                || string.IsNullOrEmpty(_userToEdit.Email))
            {
                MessageBox.Show("No fields can be empty.");
                inputError = true;
            }

            if (inputError == false)
            {
                string password = passwordBox.Password;

                if (canSave == false)
                {
                    MessageBox.Show("You can't change the Username.");
                }
                else
                {
                    // Have to grab the password
                    if (!string.IsNullOrEmpty(password))
                    {
                        _userToEdit.Password = StringCipher.Encrypt(password, Settings.GetSetting("cipherpass"));
                        MessageBox.Show("Password set to be changed.");
                    }

                    if (_isOld == false)
                    {
                        saved = _userToEdit.Save();
                    }
                    else
                    {
                        saved = _userToEdit.Save(true);
                    }

                    // Get a reference to the Window that contained the sender Control
                    if (saved == false)
                    {
                        MessageBox.Show("This was not saved properly.");
                    }
                    else
                    {
                        Window window = Window.GetWindow(sender);

                        window.Close();
                    }
                }
            }
        }
        #endregion

        #region **CanRun Functions**
        protected bool CanCancel(Control sender)
        {
            return true;
        }

        protected bool CanSave(Control sender)
        {
            return true;
            //if (_userToEdit.Username == (App.Current.MainWindow as MainWindow).LoggedInUser.Username
            //    || _userToEdit.Username == null)
            //{
            //    return true;
            //}
            //else
            //{
            //    MessageBox.Show("You can't edit this user.");
            //    return false;
            //}
        }
        #endregion
    }
}
